import cv2, time, random, os, datetime
import os, sys, pyautogui, traceback
import numpy as np
import mss
import action
import random

# from datetime import datetime
import action2

mode = 1
# 检测系统
print('操作系统:', sys.platform)
if sys.platform == 'darwin':
    scalar = True
else:
    scalar = False

# 读取文件 精度控制   显示名字
imgs = action.load_imgs()
# pyautogui.PAUSE = 0.05
pyautogui.FAILSAFE = False

start_time = time.time()
# print('程序启动，现在时间', time.ctime())

# 截屏，并裁剪以加速
upleft = (0, 0)
if scalar == True:
    downright = (1136, 700)
else:
    downright = (1900, 1080)
a, b = upleft
c, d = downright
monitor = {"top": b, "left": a, "width": c, "height": d}
# print(monitor)
start = time.time()
accounts = {
    'account_1_qiang': ['1_1_piaoliu', '1_2_qingshan', '1_3_wuming', '1_4_linyuan'],
    'account_2_jian': ['2_1_anning', '2_2_lvshui', '2_3_anning_xiao', '2_4_hexi'],
}
actions = ['takeJiaCheng', 'takeTili', 'yinyangliao', 'fengmo', 'email']

# constants
last_click = None

count = 0
count2 = 0


# 以上启动，载入设置
##########################################################
def select_mode():
    global start
    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("运行时间：{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds))
    print(datetime.datetime.now())
    huanhao()
    # yinyangliao()
    # yinyangliao()
    # huanhao()
    # click("account_2_jian")
    # click("sub_account_2_1_anning")
    # click("sub_account_2_2_lvshui")
    # click("sub_account_2_3_anning_xiao")
    # click("sub_account_2_4_hexi")

    return


def click2(ww=0, hh=0, sec=0):
    action.click([ww, hh])
    t2 = random.randint(140, 160) / 100
    time.sleep(t2 + sec)
    return True


def click3(ww1=0, ww2=0, hh1=0, hh2=0, sec=0):
    ww = random.randint(ww1, ww2)
    hh = random.randint(hh1, hh2)
    t2 = random.randint(100, 120) / 100
    time.sleep(t2)
    action.click([ww, hh])
    t2 = random.randint(140, 160) / 100
    time.sleep(t2 + sec)
    return True


def click(img, sec=0, break_count=3):
    for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]:
        screen = action.screenshot(monitor)
        want = imgs[img]
        pts = action.locate(screen, want, 0)
        if not len(pts) == 0:
            size = want[0].shape
            h, w, ___ = size
            xx = action.cheat(pts[0], w, h)
            time.sleep(1.5)
            action.click(xx)
            t2 = random.randint(140, 160) / 100
            time.sleep(t2 + sec)
            time.sleep(1.5)
            return True
        else:
            if i == break_count:
                print("没有找到" + img)
                return False
            time.sleep(.5)
def check (img, sec=0, break_count=6):
    for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]:
        screen = action2.screenshot(monitor)
        want = imgs[img]
        pts = action2.locate(screen, want, 0)
        if not len(pts) == 0:
            return True
        else:
            if i == break_count:
                print("没有找到" + img)
                return False
            time.sleep(.5)

# 领取体力
def yyl_tili():
    print("begin领取体力")
    click("yyl_tili_1")  # 点击体力
    click("yyl_tili_2_take")  # 取出体力
    click("yyl_tili_3_close")  # 关闭蒙板
    last = click("yyl_tili_4_close")  # 关闭领取体力
    if last:
        print("领取体力成功")
    else:
        print("！！！！领取体力失败！！！！")


# 领取勾玉
def yyl_gouyu():
    print("begin领取勾玉")
    last = click("yyl_gouyu_1")
    if last:
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")


# 放结界卡
def yyl_put_card():
    print("begin放结界卡")
    click("yyl_put_card_1_begin")  # 开始放结界卡
    click("yyl_put_card_2_all")  # 点击全部
    click("yyl_put_card_3_select_all_taigu")  # 选择所有太鼓
    click("yyl_put_card_4_select_one_taigu")  # 选择其中一个太鼓
    click("yyl_put_card_5_active_taigu")  # 激活太鼓
    last = click("yyl_put_card_6_end")  # 关闭
    if last:
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")


# 寄养
def yyl_jiyang():
    for i in [1, 2, 3, 4, 5, 6]:
        if click("yyl_jiyang_1_begin"):  # 寄养开始
            break
        else:
            continue
    click("yyl_jiyang_2_entry")  # 进入寄养
    success = click("yyl_jiyang_3_select_gouyu")  # 选择勾玉
    if not success:
        success = click("yyl_jiyang_4_select_tili")  # 选择体力
    if not success:
        success = click("yyl_jiyang_5_select_jinbi")  # 选择金币
    if success:
        click("yyl_jiyang_6_entry_jiejie")
        click("yinyangliao_12_4_fangshishen")
        click("yinyangliao_12_5_fangshishenjieshu")
        click("yinyangliao_12_6_fanhui")
        click("yinyangliao_12_7_fanhui")
        click("yinyangliao_12_8_fanhui")
        click("yinyangliao_16_fanhui")

    else:
        click("yinyangliao_13_fail_fanhui")
        click("yinyangliao_14_fanhui")
        click("yinyangliao_15_fanhui", 2)
        click("yinyangliao_16_fanhui")

        print(2)
    click("yinyangliao_16_fanhui")


def yinyangliao():
    # if not check("check_yinyangliao"):
    # 展开
    click3(1370, 1400, 640, 670, 2)
    #进入阴阳寮
    click3(615, 659, 614, 660, 4)
    #进入结界
    click3(1070, 1120, 628, 678, 2)
    #领取体力
    click3(950, 975, 440, 470,1)
    #领取成功
    click3(720, 800, 520, 550,1)
    # 关闭
    click3(1078, 1100, 145, 175)
    click3(1078, 1100, 145, 175)
    #收益
    click3(1020, 1050, 155, 190)
    click3(1020, 1050, 155, 190)
    #开始放结界卡
    click3(1020, 1030, 308, 400)
    #选择全部
    click3(500, 600, 110, 140)
    #选择太鼓
    click3(500, 600, 250, 280)
    #选择一个卡
    click3(350, 550, 190, 250)
    #激活
    click3(1180, 1240, 580, 620)
    #关闭
    click3(1280, 1305, 100, 125)
    #返回
    click3(105, 130, 30, 57,5)
    click3(105, 130, 20, 40, 5)
    # 关闭
    click3(1370, 1400, 640, 670, 2)



# 点击不好确认的图片
def mutil_click(imgs, sec=0, break_count=15):
    # random.shuffle(imgs)
    for img in imgs:
        success = click(img, sec, break_count)
        if success:
            return success
    return False


def switch(account, sub_account):
    t = random.randint(4, 5)
    time.sleep(t)
    # 点击头像
    print("点击头像")
    click3(100, 160, 44, 94)

    # 用户中心
    print("用户中心")
    click3(361, 394, 443, 478)
    # 点击切换账号按钮
    print("点击切换账号按钮")
    click3(1250, 1400, 150, 210, 3)
    # 点击网易图标选择账号
    print("点击网易图标选择账号")
    click3(506, 534, 310, 340)
    # 选择主账号
    print("选择主账号")
    my_min = (count - 1) * 100 + 10
    my_max = (count - 1) * 100 - 10
    click3(580, 1000, 270 + my_min, 378 + my_max)
    # 登录
    print("登录")
    click3(500, 1000, 430, 470, 8)
    # 开始切换
    print("开始切换")
    click3(560, 878, 508, 535)

    t = random.randint(5, 6)
    time.sleep(t)
    print("选择子账号名")
    # 选择子账号名
    my_min = (count2 - 1) * 200 + 10
    my_max = (count2 - 1) * 200 - 10
    click3(340 + my_min, 510 + my_max, 570, 600)
    print("选择子账号服务器")
    # 选择子账号服务器
    click3(380 + my_min, 470 + my_max, 480, 550, 3)
    print("点击进入游戏")
    # 点击进入游戏
    click3(700, 820, 580, 600)
    t = random.randint(6, 8)
    time.sleep(t)
    print("点击子账号头像")

    # 点击子账号头像
    my_min = (count2 - 1) * 135 + 20
    my_max = (count2 - 1) * 135 - 20
    print(count)
    print(130 + my_min)
    print(200 + my_max)
    click3(30, 360, 130 + my_min, 200 + my_max)
    print("点击屏幕")
    # 点击屏幕
    click3(700, 870, 560, 600)
    t = random.randint(10, 13)
    time.sleep(t)
    print("切换账号成功：" + account + "=>" + sub_account)


def common_task():
    random.shuffle(actions)
    # #点击签到
    # click3(380,400,430,440)
    # #领取签到
    # click3(725, 780, 200, 300)
    # #关闭奖励
    # click3(973, 1000, 92, 122)
    # #点击邮件
    # click3(1300, 1335, 30, 50)
    # #全部领取
    # click3(220, 250, 620, 670)
    # #确定领取
    # click3(810, 910, 400, 440)
    # #关闭奖励
    # click3(1000, 1300, 300, 600)
    # #关闭邮件
    # click3(1280, 1310, 96, 126)
    #领取勾玉
    click3(850, 880, 413, 440)
    #关闭勾玉奖励
    click3(1100, 1300, 450, 500)



def switchAccount(account, sub_account):
    print(sub_account)

    switch(account, sub_account)
    common_task()
    yinyangliao()
    # fengmo()
    return


def huanhao():
    global count
    global count2
    for account, values in accounts.items():
        count = count + 1
        count2 = 0
        for subAccount in values:
            count2 = count2 + 1
            switchAccount(account, subAccount)


########################################################
def fengmo():
    # 封魔入口
    click3(85, 115, 275, 325)
    # 参加封魔
    click3(400, 700, 150, 250)
    # 前往封魔
    click3(720, 780, 500, 535,4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)

    # 领取奖励
    click3(1380, 1410, 220, 250, 4)
    # 关闭领取
    click3(110, 150, 40, 54, 5)
    # 返回
    click3(110, 150, 40, 54, 5)
    # 领取成功
    return
    if not click("fengmo_1_start", 0, 25):  # 封魔入口
        click("fengmo_2_douji", 0, 25)  # 如果封魔入口图标换成斗技图标
    click("fengmo_3_canjia")  # 参加封魔
    click("fengmo_4_qianwang")  # 前往封魔
    click("fengmo_5_dianji")  # 点击第一次
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")  # 点击第二次
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")  # 点击第三次
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")  # 点击第四次
    t = random.randint(2, 3)
    time.sleep(t)
    for i in [1, 2]:  # 再随机点1到2次
        click("fengmo_5_dianji")
        if i == random.randint(1, 2):
            break
    click("fengmo_6_jiangli")  # 领取奖励
    click("fengmo_7_lingqujianglichenggong")  # 领取成功

    for i in [1, 2, 3]:
        click("fengmo_8_xunzhaoshouling")  # 寻找首领
        dayOfWeek = datetime.datetime.now().isoweekday()
        click("fengmo_9_guaishou" + str(dayOfWeek))  # 点击首领
        click("fengmo_10_jijietiaozhan")  # 集结挑战
        click("fengmo_11_jijiequeding")  # 集结确定
        t = random.randint(2, 3)
        time.sleep(t)
        meiyouzhaodao = click("fengmo_11_1_meiyouzhaodaoguaishou")  # 寻找下个首领
        if not meiyouzhaodao:
            break

    click("fengmo_11_jijiequeding")  # 开始战斗
    t = random.randint(400, 420)
    time.sleep(t)

    click("fengmo_12_1_shengli")
    click("fengmo_12_chenggong")
    click("fengmo_13_fanhui")
    click("fengmo_13_chenggong")
    click("fengmo_14_chenggong")
    click("fengmo_15_fanhui")
    click("fengmo_16_quedingtuicu")
    click("fengmo_18_fanhui")


####################################################
if __name__ == '__main__':
    select_mode()
